package com.isoft.student.service.impl;

import com.isoft.student.dao.AnswerMapper;
import com.isoft.student.dao.ClassesMapper;
import com.isoft.student.dao.PersonMapper;
import com.isoft.student.dao.StudentMapper;
import com.isoft.student.eo.StudentEOPage;
import com.isoft.student.model.Student;
import com.isoft.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现业务及流程接口当中的内容
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private PersonMapper personMapper;
    /**
     * 学生信息添加方式实现
     *
     * @param student 学生信息实体
     * @return 是否成功（-1 重复添加）
     */
    @Override
    public int AddStudnt(Student student) {
        int returnInt = 0;
        //业务判断
        //（1）学生信息名称不能重复

        //(1)判断学生信息
        int SclassCount = studentMapper.getSclassCount(student.getSclass());
        if (SclassCount < 1) {
            //2.学生信息数据添加
            int addStudentInt = studentMapper.insertStudent(student);
            //3.返回值设置
            returnInt = addStudentInt;
        } else {
            returnInt = -1;//有相同的学生信息名称
        }
        return returnInt;
    }

    /**
     * 更新学生信息
     *
     * @param student
     * @return 更新是否成功（-1：更新执行SQL报错）
     */
    @Override
    public int UpdateStudent(Student student) {
        int isUpdate = 0;
        //1.没有相关业务流程的判断
        try {
            isUpdate = studentMapper.updateStudent(student);
        } catch (Exception e) {
            isUpdate = -1;
            //打印更新报错的情况
            System.out.print("更新学生信息时执行SQL报错的原因为：" + e.getMessage());
        }
        return isUpdate;
    }

    /**
     * 学生信息删除
     * @param perid
     * @return
     */
    @Override
    public int DeleteStudent(int perid) {
       int delStudent=0;
        /*1.有关业务流程的判断
         * （1）删除教师不能与人员角表有关联
         * */
        try {
            //返回的结果总数量小于1，才可以删除
            if (personMapper.getStudentCount(perid)<1){
                delStudent=studentMapper.delStudent(perid);
            }
        }catch (Exception e){
            delStudent=-1;
            System.out.print("删除角色信息室执行SQL时报错，信息为："+e.getMessage());
        }
        return delStudent;
    }

    @Override
    public List<StudentEOPage> getStudentWhereAll(StudentEOPage studentEOPage) {
       List<StudentEOPage> listStudentEo=studentMapper.getStudentWhereAll(studentEOPage);
       return listStudentEo;
    }
}
